﻿function AzureOnlineBlobDetailsView(context) {

    var self = this;

    var dispatcher = $({});
    var blob = ko.observable(null);
    var screen = ko.observable('properties');

    var properties = new AzureOnlineBlobPropertiesView(context);
    var metadata = new AzureOnlineBlobMetadataView(context);
    var blobResize = new AzureOnlineBlobResizeView(context);

    function init(blobModel) {
        blob(blobModel);
        properties.init(blobModel);
        metadata.init(blobModel);
        blobResize.init(blobModel);
        if (blobModel.type() != 'PageBlob' && screen() == 'blobResize') {
            goToProperties();
        }
    }

    properties.addOnSuccess(onSuccess);
    metadata.addOnSuccess(onSuccess);
    blobResize.addOnSuccess(onSuccess);

    function submit() {
        switch (screen()) {
            case 'properties':
                properties.submit();
                break;
            case 'metadata':
                metadata.submit();
                break;
            case 'blobResize':
                blobResize.submit();
                break;
        }
    }

    function goToProperties() {
        screen('properties');
    }

    function goToMetadata() {
        screen('metadata');
    }

    function goToBlobResize() {
        screen('blobResize');
    }

    function onSuccess() {
        dispatcher.trigger('success');
    }

    self.blob = blob;
    self.init = init;
    self.submit = submit;
    self.screen = screen;
    self.goToProperties = goToProperties;
    self.goToMetadata = goToMetadata;
    self.goToBlobResize = goToBlobResize;

    self.properties = properties;
    self.metadataView = metadata;
    self.blobResizeView = blobResize;

    this.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}